require("../../assets/css/common.css");
require("../../assets/font_44fp3ls0tzq/iconfont.css");
require("../../assets/css/reset.css");
require("../../assets/css/normalize.css");
let dom = require("../../utils/dom.js");
require("./register.css");
const axios = require("axios");

//表单验证
document.addEventListener("DOMContentLoaded", function () {
	dom.get("#rgs-btn").addEventListener("click", function () {
		let error = dom.get("#error");
		let account = dom.get("#account").value;
		let reg = /^1[34567]\d{9}$/i;
		if (reg.test(account)) {
			error.textContent = "";
		} else {
			error.textContent = "手机格式有误";
			return;
		}

		// 密码验证
		let pawd = dom.get("#pawd").value;
		let reg1 = /^\w{6,12}$/i;
		if (reg1.test(pawd)) {
			error.textContent = "";
		} else {
			error.textContent = "密码输入有误";
			return;
		}

		//后台：用户名和密码验证账户验证
		uerPwd();
		async function uerPwd() {
			let res = await axios
				.post("http://fitness.h5.itsource.cn:3701/api/user/login", {
					account: account,
					password: pawd,
				})
				.then((res) => {
					if (res.data.errno === 0) {
						localStorage.setItem("token", res.data.data.token);
						alert("登录成功");
						location.href = "index.html";
					} else {
						error.textContent = res.data.message;
					}
				});
		}
	});
});
